﻿//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//#include <assert.h>

#include "common.h"

void* my_memcpy(void* dest, const void* src, size_t count)
{
	assert(dest && src);
	void* ret = dest;
	while (count--)
	{
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}

void* my_memmove(void* dest, const void* src, size_t count)
{
	assert(dest && src);
	void* ret = dest;
	if (dest > src)
	{
		dest = (char*)dest + count - 1; // 指向最后一个字节
		src = (char*)src + count - 1; // 指向最后一个字节
		while (count--)
		{ // 从后往前复制
			*(char*)dest = *(char*)src;
			if (dest > ret)
			{
				dest = (char*)dest - 1;
				src = (char*)src - 1;
			}
			else
				break; // 防止指针向前偏移越界
		}
	}
	else
		while (count--)
		{ // 从前往后复制
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	return ret;
}
